package com.ruoyi.web.controller.diagmanage;


import cn.hutool.core.io.resource.ClassPathResource;
import com.ruoyi.analysiswarning.domain.vo.PageVO;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.diagmanage.domain.DatabaseReport;
import com.ruoyi.diagmanage.domain.QueryParams;


import com.ruoyi.diagmanage.service.DataBaseService;

import com.ruoyi.web.controller.util.Export2ExcelListMap;
import org.apache.poi.hssf.usermodel.*;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

import static com.ruoyi.common.core.domain.AjaxResult.success;

@RestController
@RequestMapping("/StateDatabaseController")
public class StateDatabaseController {

    @Resource
    private DataBaseService dataBaseService;

    public static String interceptTime(String timeStr) {
        if (!StringUtils.isBlank(timeStr)) {
            if (timeStr.contains("T")) {
                DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
                LocalDateTime ldt = LocalDateTime.parse(timeStr, df);
                ZoneId currentZone = ZoneId.of("UTC");
                ZoneId newZone = ZoneId.of("Asia/Shanghai");
                timeStr = ldt.atZone(currentZone).withZoneSameInstant(newZone).toLocalDateTime().toString();
            }
            if (timeStr.length() >= 10) {
                return timeStr.substring(0, 10);
            }
        }
        return timeStr;
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.setDisallowedFields(new String[]{"admin"});
    }

    @Log(title = "深度分析报告查询", businessType = BusinessType.QUERY)
    @PostMapping("/queryStateReportList")
    public AjaxResult queryDatabaseReportList(@RequestBody PageVO<DatabaseReport> databaseReportPageVO) {
        return success(dataBaseService.queryDatabaseReportList(databaseReportPageVO));
    }

    @PreAuthorize("@ss.hasPermi('system:user:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response,DatabaseReport databaseReport) throws IOException {
        List<Map<String, Object>> databaseReports = dataBaseService.queryDatabaseReportList(databaseReport);
        List<String> nameList = Arrays.asList("所属数据库", "报告描述","诊断时间","操作人");
        Export2ExcelListMap.export(response,nameList,databaseReports,"databaseName",
                "describe","diagnosisTime","operator");

    }

    //建转运硬件设备管理  导出
    @PostMapping("/export2")
    public void export2(HttpServletResponse response, QueryParams queryParams) throws IOException {
        //List<DatabaseReport> databaseReports = dataBaseService.queryDatabaseReportList(databaseReport);


        //ExcelUtil<DatabaseReport> util = new ExcelUtil<DatabaseReport>(DatabaseReport.class);


        //util.exportExcel(response, databaseReports, "数据库分析结果");

        //将你创建好的excel文件放到静态文件夹下面然后引用
        ClassPathResource classPathResource = new ClassPathResource("static/123.xls");
        InputStream inputStream = classPathResource.getStream();
        HSSFWorkbook workBook = new HSSFWorkbook(inputStream);


        HSSFSheet sheet1 = workBook.getSheetAt(0);//获取第一个sheet页，也就是正常填充业务数据的sheet页
        HSSFRow row0 = sheet1.getRow(0);
        HSSFCell cell0 = row0.createCell(0);

        cell0.setCellValue(queryParams.getTableName());

        HSSFRow row1 = sheet1.getRow(1);
        row1.createCell(1);
        HSSFCell cell = row1.getCell(1);
        cell.setCellValue(queryParams.getWinVer());
        row1.createCell(3);
        HSSFCell cell2= row1.getCell(3);
        cell2.setCellValue(queryParams.getMem());
        row1.createCell(5);
        HSSFCell cell3= row1.getCell(5);
        cell3.setCellValue(queryParams.getDisk());

        HSSFRow row2 = sheet1.getRow(2);
        row2.createCell(1);
        HSSFCell row21 = row2.getCell(1);
        row21.setCellValue(queryParams.getCheckMsg());
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("表名.xls", "UTF-8"));
        ServletOutputStream out = response.getOutputStream();
        workBook.write(out);
        out.close();
        workBook.close();

    }






}
